package org.home.millionaire;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;

/**
 * Created by Duong Binh Cuong on 5/19/13.
 */
public class Main extends Activity {
	Button btnExit;
	Button btnNew;
	Button btnInfo;
	Button btnSettings;

	MediaPlayer player;
	SharedPreferences prefs;

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(player != null) {
			player.stop();
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(player != null) {
			player.stop();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(player != null) {
			if (!prefs.getBoolean("checkbox_sound", true)) {
				player.stop();
			} else {
				if(!player.isPlaying()) {
					try {
						player.prepare();
						player.start();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					}
				}
			}
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		prefs = PreferenceManager.getDefaultSharedPreferences(Main.this);

		btnExit = (Button) findViewById(R.id.btnExit);
		btnInfo = (Button) findViewById(R.id.btnInfo);
		btnNew = (Button) findViewById(R.id.btnNew);
		btnSettings = (Button) findViewById(R.id.btnSettings);

		if (prefs.getBoolean("checkbox_sound", true)) {
			player = MediaPlayer.create(this, R.drawable.music);
			player.setLooping(true);
			player.setVolume(100, 100);
			player.start();
		}

		btnExit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if(player != null) {
					player.stop();
				}
				finish();
			}
		});

		btnSettings.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(Main.this, Settings.class);
				startActivity(intent);
			}
		});

		btnInfo.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				AlertDialog infoDialog = createInfoDialog();
				infoDialog.show();
			}
		});

		btnNew.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub

				if(player != null) {
					player.stop();
				}

				Intent intent = new Intent(Main.this, Game.class);
				startActivity(intent);
			}
		});
	}

	private AlertDialog createInfoDialog() {
		AlertDialog infoDialog = new AlertDialog.Builder(this)
				.setIcon(R.drawable.logo)
				.setTitle("Giới thiệu")
				.setMessage(
						"Game: Ai Là Triệu Phú\nTác giả: Lê Minh Cường, Dương Bính Cường")
				.setPositiveButton("Đóng", null).create();
		return infoDialog;
	}
}